//
//  LLSlider.m
//  LLUIWrapper
//
//  Created by massimo on 2017/4/15.
//  Copyright © 2017年 kissnger. All rights reserved.
//

#import "LLSlider.h"

#define BlockMinX 2.f

void addText(CGRect rect ,NSString *text, UIColor *textColor,UIFont *font,UIImage *arrow);
void addBlock(CGRect rect , UIImage *image ,CGFloat positionX);

@interface LLSlider()
@property (nonatomic ,assign) CGFloat lastX;
@property (nonatomic ,assign) CGFloat slidPositionX;
@property (nonatomic ,assign) BOOL isSlid;
@property (nonatomic ,assign) BOOL isSlidFinished;
@end


@implementation LLSlider
- (void)drawRect:(CGRect)rect{
  CGContextRef context = UIGraphicsGetCurrentContext();
  [self.backgroundColor setFill];
  CGContextFillRect(context, rect);
  CGContextFillPath(context);
  UIFont *font = [UIFont boldSystemFontOfSize:self.placeholderFont];
  
  addText(rect, self.placeholder,self.placeholderColor,font,self.arrowImage);
  
  
  
  
  CGContextSaveGState(context);
  [SlidPrgressColor setFill];
  
  CGFloat filletRadius = CGRectGetHeight(self.bounds)/2.0  - BlockMinX;
  CGFloat slidProgress = self.isSlid ? (self.slidPositionX + filletRadius * 2 + BlockMinX) : 0 ;
  CGFloat minx = CGRectGetMinX(rect);
  CGFloat midx = slidProgress/2.0;
  CGFloat maxx = slidProgress;
  
  CGFloat miny = CGRectGetMinY(rect);
  CGFloat midy = CGRectGetMidY(rect);
  CGFloat maxy = CGRectGetMaxY(rect);
  CGContextMoveToPoint(context, minx, midy);
  CGContextAddArcToPoint(context, minx, miny, midx, miny, filletRadius);
  CGContextAddArcToPoint(context, maxx, miny, maxx, midy, filletRadius);
  CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, filletRadius);
  CGContextAddArcToPoint(context, minx, maxy, minx, midy, filletRadius);
  CGContextFillPath(context);
  CGContextRestoreGState(context);
  
  
  addBlock(rect, self.blockImage, self.slidPositionX);
  self.layer.cornerRadius = CGRectGetHeight(rect)/2.0;
  self.layer.masksToBounds = YES;
}

#pragma mark - Draw (c func )

//绘制文字 以及箭头
void addText(CGRect rect ,NSString *text, UIColor *textColor, UIFont *font,UIImage *arrow) {
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSaveGState(context);
  //1.  绘制 text
  //text的样式； 
  NSDictionary *attr = @{NSFontAttributeName:font,
                         NSForegroundColorAttributeName:textColor};
  //text的size
  CGSize textSize = [text sizeWithAttributes:attr];
  CGFloat tX = (CGRectGetWidth(rect) - textSize.width - arrow.size.width)/2.0;
  CGFloat tY = (CGRectGetHeight(rect) - textSize.height)/2.0;
//  text的Rect
  CGRect pRect = CGRectMake(tX, tY, textSize.width, textSize.height);

  [text drawInRect:pRect withAttributes:attr];
  
  //2. 绘制箭头
  //箭头的大小
  CGSize arrowSize = arrow.size;
  CGFloat iX = tX + 5 + textSize.width;
  CGFloat iY = (CGRectGetHeight(rect) - arrowSize.height)/2.0;
  
  CGRect imageRect = CGRectMake(iX, iY, arrowSize.width, arrowSize.height);
  [arrow drawInRect:imageRect];
  
  CGContextRestoreGState(context);
  CGContextFillPath(context);
}
//绘制 滑块

void addBlock(CGRect rect , UIImage *image ,CGFloat positionX){
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSaveGState(context);
  
  CGFloat space = BlockMinX;
  CGFloat x = positionX;
  CGFloat y = space;
  
  //画圆
  CGFloat radius = CGRectGetHeight(rect) - space *2 ;
  CGRect ellRect = CGRectMake(x, y, radius, radius);
  CGContextAddEllipseInRect(context, ellRect);
  //剪裁
  CGContextClip(context);
  CGContextFillPath(context);
  //绘制图片
  [image drawInRect:ellRect];
  
  CGContextRestoreGState(context);
  CGContextFillPath(context);
}


#pragma mark - touch

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  if (self.isSlid == YES)  return;
  
  CGPoint p = [touches.anyObject locationInView:self];
  CGFloat radius = CGRectGetHeight(self.bounds)  - 2 * BlockMinX;
  CGRect beginRect = CGRectMake(BlockMinX, BlockMinX, radius, radius);
  
  self.isSlid = CGRectContainsPoint(beginRect, p);
  self.lastX = p.x;
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  
  if (self.isSlid == YES) {
    CGPoint p = [touches.anyObject locationInView:self];
    CGFloat blockMaxX = CGRectGetWidth(self.bounds) - CGRectGetHeight(self.bounds) + BlockMinX;
    if (p.x >= blockMaxX) {
      self.isSlidFinished = YES;
      if (self.delegate && [self.delegate respondsToSelector:@selector(slideEnd:)]) {
        [self.delegate performSelector:@selector(slideEnd:) withObject:self];
      }
    }
  }
  self.slidPositionX = BlockMinX;
  self.isSlid = NO;
  self.lastX = 0;
  [UIView animateWithDuration:0.3 animations:^{
    [self setNeedsDisplay];
  }];
  
  
}


- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  if (!self.isSlid) return;
  
  
  CGPoint p = [touches.anyObject locationInView:self];
 
  CGFloat dddd = p.x - self.lastX;
  self.slidPositionX += dddd;
  CGFloat blockMaxX = CGRectGetWidth(self.bounds) - CGRectGetHeight(self.bounds) + BlockMinX;
  
  if (self.slidPositionX < BlockMinX) self.slidPositionX = BlockMinX;
  if (self.slidPositionX > blockMaxX) self.slidPositionX = blockMaxX;
  self.lastX = p.x;
  [self setNeedsDisplay];
}


#pragma mark - getter setter

- (NSString *)placeholder{
  if (_placeholder == nil) {
    _placeholder = @"请右划";
  }
  return _placeholder;
}


- (UIColor *)placeholderColor{
  if (_placeholderColor == nil) {
    _placeholderColor = [UIColor blackColor];
  }
  return _placeholderColor;
}


- (CGFloat)placeholderFont{
  if (_placeholderFont < 12) {
    _placeholderFont = 12;
  }
  return _placeholderFont;
}

- (CGFloat)slidPositionX{
  if (_slidPositionX < 2) {
    _slidPositionX = BlockMinX;
  }
  return _slidPositionX;
}
@end
